package yxy.game.pm2.module.union.boss;

import yxy.apple.util.redisson.RedisException;
import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.unionboss;
import yxy.game.pm2.module.union.UnionBean;
import yxy.game.pm2.module.union.UnionModule;

public class UnionBoss extends UnionModule {

	// bean===========================================================================

	private int dataId;// Boss id
	private long hp;// 血量
	private long residual;// 残血

	public int getDataId() {
		return dataId;
	}

	public void setDataId(int dataId) {
		this.dataId = dataId;
	}

	public long getHp() {
		return hp;
	}

	public void setHp(long hp) {
		this.hp = hp;
	}

	public long getResidual() {
		return residual;
	}

	public void setResidual(long residual) {
		this.residual = residual;
	}

	// func==========================================================================

	public unionboss data() {
		return DataCenter.getData(getDataId(), unionboss.class);
	}

	public void nextboss() {
		dataId += 1;
		unionboss data = data();
		if (data == null) {
			return;
		}
		long hp = Long.valueOf(data.getLife()[0]) * data.getLife()[1];
		setDataId(data.getId());
		setHp(hp);
		setResidual(hp);
		set();
	}

	// redis==========================================================================

	static private String key(String unionCode) {
		return String.format("UNION:%s:MODULE:BOSS", unionCode);
	}

	public void set() {
		try {
			setv(key(getUnionCode()), this);
		} catch (RedisException e) {
			e.printStackTrace();
		}
	}

	static public UnionBoss get(UnionBean union) {
		try {
			UnionBoss module = getv(key(union.getCode()));
			if (module == null) {
				module = create(union);
			}
			return module;
		} catch (RedisException e) {
			e.printStackTrace();
		}
		return null;
	}

	private static UnionBoss create(UnionBean union) {
		UnionBoss module = new UnionBoss();
		module.setUnionCode(union.getCode());
		module.nextboss();
		return module;
	}

	// lock==========================================================================

	@Override
	public String lockKey() {
		return String.format("lock:Union:%s", key(getUnionCode()));
	}
	
	@Override
	public boolean tryLock() throws InterruptedException {
		return super.tryLock();
	}
}
